import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:hrf_app/common/extension/ex_visibility.dart';
import 'package:hrf_app/pages/menu/tool/image_water/imageWater/widgets/FileVoModel.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'index.dart';

class ImageCompanyWaterPage extends GetView<ImageCompanyWaterController> {
  const ImageCompanyWaterPage({super.key});

  Widget _buildView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        _buildShowImage()
            .toVisibility(visible: controller.fileList.length != 0),
      ],
    );
  }

//展示图片

  Widget _buildShowImage() {
    Widget fildUpload = Container(
        margin: const EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(3),
        ),
        child: Column(
          children: [
            Container(
                margin: EdgeInsets.symmetric(vertical: 5),
                child: GridView(
                  ///子Item排列规则
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                      //横轴元素个数
                      crossAxisCount: 3,
                      //纵轴间距
                      mainAxisSpacing: 2.0,
                      //横轴间距
                      crossAxisSpacing: 2.0,
                      //子组件宽高长度比例
                      childAspectRatio: 0.82),

                  ///GridView中使用的子Widegt
                  children: buildListViewItemList(),
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                )),
          ],
        ));
    return fildUpload;
  }

  List<Widget> buildListViewItemList() {
    List<Widget> itemList = [];
    for (var i = 0; i < controller.fileList.length; i++) {
      FileVoModel itemVo = controller.fileList[i];
      Widget item = GestureDetector(
        child: _imgDetailWidget(i, itemVo),
        onTap: () {
          controller.imgView(i);
        },
      );
      itemList.add(item);
    }
    return itemList;
  }

  Widget _imgDetailWidget(int i, FileVoModel itemVo) {
    return Column(children: [
      Container(
        color: Colors.transparent,
        alignment: Alignment.center,
        child: Stack(alignment: Alignment.topRight, children: <Widget>[
          SizedBox(
            child: Image.file(
              itemVo.addWaterFile!,
              width: 110,
              height: 110,
              fit: BoxFit.cover,
            ),
          ),
          GestureDetector(
            child: Icon(TDIcons.close_circle_filled),
            onTap: () {
              controller.delImage!(i);
            },
          ),
        ]),
      ),
      TDButton(
        text: "编辑",
        size: TDButtonSize.small,
        type: TDButtonType.text,
        shape: TDButtonShape.rectangle,
        theme: TDButtonTheme.primary,
        onTap: () {
          controller.editItem(itemVo);
        },
      )
    ]);
  }

  Widget _buildSaveMaterial() {
    return ListView(
      children: [
        Column(children: [
          Container(
              height: 60,
              alignment: Alignment.center,
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    TDButton(
                      text: "保存配件",
                      theme: TDButtonTheme.primary,
                      onTap: () {
                        controller.saveMaterialDialog();
                      },
                    ),
                    TDButton(
                      text: "上传",
                      theme: TDButtonTheme.primary,
                      onTap: () {
                        controller.selectImageOnTap();
                      },
                    ),
                  ]))
        ]),
        Column(children: [
          Container(
              height: 60,
              alignment: Alignment.center,
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    TDButton(
                        text: "带水印下载",
                        theme: TDButtonTheme.primary,
                        onTap: () {
                          controller.downloadImagesToLocalByWater();
                        }),
                    TDButton(
                        text: "无水印下载",
                        theme: TDButtonTheme.primary,
                        onTap: () {
                          controller.downloadImagesToLocal();
                        }),
                  ]))
        ])
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImageCompanyWaterController>(
      init: ImageCompanyWaterController(),
      id: "imageCompanyWater",
      builder: (_) {
        return Scaffold(
            appBar: AppBar(title: const Text("添加公司水印")),
            body: SafeArea(
              child: Column(children: [
                Expanded(child: _buildView()),
                BottomAppBar(
                  height: 150,
                  child: _buildSaveMaterial(),
                )
              ]),
            ));
      },
    );
  }
}
